#pragma once

#include <string>
#include <vector>
typedef struct _CPhidget *CPhidgetHandle;

namespace Phiol
{

/*
	Device

	The Device is the base class for all Phidget devices

	User guide
	http://www.phidgets.com/docs/General_Phidget_Programming

	C-API reference
	http://www.phidgets.com/documentation/web/cdoc/index.html
	http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html
*/
class Device
{
public:
	virtual void		update() {}
	
	enum Type
	{
		kSpatial,
		kTemperatureSensor
	};

	Type				getType() const				{ return mType; }	

	CPhidgetHandle		getPhidgetHandle() const	{ return mPhidgetHandle; }
	const std::string&	getName() const				{ return mName; }
	int					getSerialNumber() const		{ return mSerialNumber; }
	int					getVersion() const			{ return mVersion; }
	bool				isAttached() const;		
	const std::string&	getTypeName() const			{ return mTypeName; }
	const char*			getLabel() const;
	void				setLabel( const char* label );
	
	class Listener
	{
	public:
		virtual ~Listener() {}
		virtual void onDeviceChanged( Device* /*device*/ ) {}
	};

	void				addListener( Listener* listener );
	bool				removeListener( Listener* listener );
	void				removeListeners();
	
	virtual std::string toString() const;

protected:
	Device( Type type, CPhidgetHandle phidgetSpecificHandle );
	
	friend class DeviceManager;
	virtual ~Device();

	CPhidgetHandle		getPhidgetHandleFromManager() const { return mPhidgetHandleFromManager; }
	void				setPhidgetHandleFromManager( CPhidgetHandle phidgetHandle ) { mPhidgetHandleFromManager = phidgetHandle; }
	
	void				getInformation();
	
	typedef				std::vector<Listener*> Listeners; 
	const Listeners&	getListeners() const { return mListeners; }	

private:
	CPhidgetHandle		mPhidgetHandleFromManager;
	CPhidgetHandle		mPhidgetHandle;
	Type				mType;
	std::string			mName;
	int					mSerialNumber;
	int					mVersion;
	std::string			mTypeName;
	Listeners			mListeners;
};

}